/**
 * 无害化处理 已处理
 * @constructor
 */
function HarmlessReportProcessed() {
}
let img = "";
HarmlessReportProcessed.init = function () {

    let vm = new Vue({
        el: "#customModule",
        data: {
            //实体
            harmlessReportID: "",
            dead: "",
            createTime: "",
            animal: "",
            enterpriseName: "",
            driver: "",
            car: "",
            phone: "",
            cause: "",
            code: "",
            person: "",
            method: 0,
            userName: "",
            detailedAddress: "",
            ephone: "",
            user:"",
            dhannel:"",
            transportationTime:"",
            processingTime:"",
            img:"",
            //搜索值
            enterpriseNameCondition: "",
            timeInterval: "",
            //附加属性
            clickState: "",
            roleID: "",
            isAdd: "true",
            roleList: ""
        },
        created() {
            //日期区间选择
            laydate.render({
                elem: '#immuneTime',
                theme: 'molv',
                type: 'date',
                done: function (value) {
                    vm.immuneTime = value;
                }
            });
            laydate.render({
                elem: '#search_TimeInterval',
                range: true,
                done:function (value) {
                    console.log("时间选择器",value.split(" - "))
                    vm.timeInterval = value;
                }
            });
            laydate.render({
                elem: '#validity',
                theme: 'molv',
                type: 'date',
                done: function (value) {
                    vm.validity = value;
                }
            });
            this.roleID = $.cookie('RoleIds');
            self = this;
            window.show = self.show;
            window.edit = self.edit;
            window.treatment = self.treatment;
            window.transport = self.transport;
        },
        mounted() {
            this.initBootStrapSource();
        },
        methods: {
            initBootStrapSource() {
                $('#tableList').bootstrapTable({
                    url: '/harmlessReportProcessed/tableSearch',
                    method: 'post',
                    toolbar: "#toolbar",
                    sidePagination: 'server',
                    dataField: "data",
                    striped: true,
                    pagination: true,
                    pageNumber: 1,
                    pageSize: 10,
                    pageList: [10, 25, 50, 100],
                    sortable: true,
                    sortName: 'createDateTime',
                    sortOrder: "desc",
                    search: false,
                    showRefresh: true,
                    showColumns: true,
                    showExport: true,
                    showToggle: true,
                    queryParamsType: 'undefined',
                    queryParams: function queryParams(params) {
                        return {
                            pageSize: params.pageSize,      //页面大小
                            pageIndex: params.pageNumber,   //页码
                            sort: params.sortName,      //排序列名
                            sortOrder: params.sortOrder, //排位命令（desc，asc）
                            enterpriseName: $("#enterpriseNameCondition").val(),
                            timeInterval:$("#search_TimeInterval").val()
                        };
                    },
                    onLoadSuccess: function (data) {
                        //Help.alertSuccMessage("数据加载完毕");
                    },
                    onLoadError: function (status) {
                        // Help.alertErrorMessage("数据加载出错");
                    },
                    columns: [
                        {
                            title: '序号',//标题  可不加
                            align: "center",
                            width: 40,
                            formatter: function (value, row, index) {
                                var pageSize = $('#tableList').bootstrapTable('getOptions').pageSize;//通过表的#id 可以得到每页多少条
                                var pageNumber = $('#tableList').bootstrapTable('getOptions').pageNumber;//通过表的#id 可以得到当前第几页
                                return pageSize * (pageNumber - 1) + index + 1;//返回每条的序号： 每页条数 * （当前页 - 1 ）+ 序号
                            }
                        },
                        {field: 'createTime', title: '上报日期', align: 'center'},
                        {field: 'enterpriseName', title: '上报企业', align: 'center'},
                        {field: 'animal', title: '病死畜禽种类', align: 'center'},
                        {field: 'dead', title: '病死畜禽数量', align: 'center'},
                        {field: 'cause', title: '死亡原因', align: 'center'},
                        // {field: 'code', title: '病死畜禽编号', align: 'center'},
                        // {
                        //     field: 'dhannel', title: '处理途径', align: 'center',
                        //     formatter: function (value) {
                        //         let s;
                        //         if (value === 0) {
                        //             s = "自行处理";
                        //         } else if (value === 1) {
                        //             s = "集中处理";
                        //         }
                        //         return s;
                        //     }
                        // },
                        {
                            field: 'method', title: '处理方式', align: 'center',
                            formatter: function (value) { // 0焚烧、1深埋、2化制、3集中无害化处理
                                let s;
                                if (value === 0) {
                                    s = "焚烧";
                                } else if (value === 1) {
                                    s = "深埋";
                                } else if (value === 2) {
                                    s = "化制";
                                } else if (value === 3) {
                                    s = "集中无害化处理";
                                }
                                return s;
                            }
                        },
                        {field: 'transportationTime', title: '运输时间', align: 'center'},
                        {field: 'processingTime', title: '处理时间', align: 'center'},
                        {
                            field: 'status', title: '状态', align: 'center',
                            formatter: function (value) {
                                let s;
                                if (value === 0) {
                                    s = "待上报";
                                } else if (value === 1) {
                                    s = "待运输";
                                } else if (value === 2) {
                                    s = "待处理";
                                } else if (value === 3) {
                                    s = "已处理";
                                }
                                return s;
                            }
                        },
                        {
                            title: '操作', align: 'center',
                            formatter: function (value, row, index) {
                                let optionHtml = "";
                                if (row.status === 1) { //待运输
                                    optionHtml = "<a class=\"btn btn-warning btn-xs\"  href=\"javascript:void(0)\"  onclick='transport(" + JSON.stringify(row) + ")'  title=\"运输\" ><i class=\"fa fa-edit\"></i>运输</a>";
                                } else if (row.status === 2) { //待处理
                                    optionHtml = " <a class=\"btn btn-success btn-xs\" href=\"javascript:void(0)\"  onclick='treatment(" + JSON.stringify(row) + ")' title=\"处理\"><i class=\"fa fa-edit\"></i>处理</a>";
                                }
                                optionHtml += " <a class=\"btn btn-info btn-xs\" href=\"javascript:void(0)\"  onclick='show(" + JSON.stringify(row) + ")' title=\"查看详情\">查看详情</a>";
                                return optionHtml;
                            }
                        }
                    ]
                });
            },
            /**
             * 查询
             */
            search() {
                vm.refresh();
            },
            /**
             * 重置
             */
            reset() {
                vm.enterpriseNameCondition = "";
                $("#enterpriseNameCondition").val("");
                vm.timeInterval = "";
                $("#search_TimeInterval").val("");
                vm.refresh();
            },
            /**
             * 编辑
             * @param data
             */
            edit(data) {
                vm.harmlessReportID = data.harmlessReportID;
                vm.dead = data.dead;
                vm.clickState = "edit";
                $('#myModalAdd').modal('show');
            },
            /**
             * 查看详情
             * @param data
             */
            show(data) {
                vm.animal = data.animal;
                vm.dead = data.dead;
                vm.user = data.user;
                vm.createTime = data.createTime;
                vm.cause = data.cause;
                vm.code = data.code;
                vm.dhannel = data.dhannel;
                vm.driver = data.driver;
                vm.transportationTime = data.transportationTime;
                vm.car = data.car;
                vm.phone = data.phone;
                vm.method = data.method;
                vm.person = data.person;
                vm.processingTime = data.processingTime;
                vm.enterpriseName = data.enterpriseName;
                vm.userName = data.userName;
                vm.detailedAddress = data.detailedAddress;
                vm.ephone = data.ephone;
                vm.img = data.img;
                $('#myModalAdd3').modal('show');
            },
            /**
             * 运输
             */
            transport(data) {
                vm.harmlessReportID = data.harmlessReportID;
                vm.createTime = data.createTime;
                vm.dead = data.dead;
                vm.enterpriseName = data.enterpriseName;
                vm.animal = data.animal;
                vm.driver = "";
                vm.car = "";
                vm.phone = "";
                $('#myModalAdd1').modal('show');
            },
            /**
             * 处理
             * @param data
             */
            treatment(data) {
                vm.harmlessReportID = data.harmlessReportID;
                vm.createTime = data.createTime;
                vm.dead = data.dead;
                vm.animal = data.animal;
                vm.cause = data.cause;
                vm.code = "";
                vm.method = 0;
                vm.driver = data.driver;
                vm.car = data.car;
                vm.phone = data.phone;
                vm.person = "";
                //显示文件选择
                $("#uploadImages").css("display","");
                var file = $("#uploadImages") ;
                file.after(file.clone().val(""));
                file.remove();
                //隐藏图片
                $("#showImage").css("display","none");
                img = "";
                $('#myModalAdd').modal('show');
            },
            /**
             * 新增
             */
            addRecord() {
                vm.harmlessReportID = "";
                vm.dead = "";
                vm.clickState = "add";
                $('#myModalAdd').modal('show');
            },
            /**
             * 处理
             */
            saveRecord() {
                if (!validateForm()) return false;
                let pm = {
                    harmlessReportID: vm.harmlessReportID,
                    code: vm.code,
                    method: vm.method,
                    person: vm.person,
                    img:img
                }
                let url = "/harmlessReportTreatment/treatment";
                axios.post(url, pm).then(data => {
                    $('#tableList').bootstrapTable(('refresh'));
                    if (data.data.code === "200") {
                        $('#myModalAdd').modal('hide')
                        swal("操作成功!", "请继续操作", "success");
                        this.refresh();
                    }
                }).catch(e => {
                    console.log(e);
                    swal("操作失败!", "请重试", "error");
                });
            },
            /**
             * 运输
             */
            saveRecord1() {
                if (!validateForm2()) return false;
                let pm = {
                    harmlessReportID: vm.harmlessReportID,
                    driver: vm.driver,
                    car: vm.car,
                    phone: vm.phone,
                }
                let url = "/harmlessReportTreatment/transport";
                axios.post(url, pm).then(data => {
                    $('#tableList').bootstrapTable(('refresh'));
                    if (data.data.code === "200") {
                        $('#myModalAdd1').modal('hide')
                        swal("操作成功!", "请继续操作", "success");
                        this.refresh();
                    }
                }).catch(e => {
                    console.log(e);
                    swal("操作失败!", "请重试", "error");
                });
            },
            refresh() {
                $('#tableList').bootstrapTable(('refresh'));
            }
        }
    });
};

//处理 表单验证
function validateForm() {
    //validate方法参数可选
    return $("#myform").validate({
        rules: {
            code: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            person: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
        },
        //错误信息提示
        messages: {
            code: {
                required: "(必填)",
                minlength: "最少1位",
            },
            person: {
                required: "(必填)",
                minlength: "最少1位",
            },
        },
    }).form();
}

//运输 表单验证
function validateForm2() {
    //validate方法参数可选
    return $("#myform1").validate({
        rules: {
            driver: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            car: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
            phone: {
                required: true,//必填
                minlength: 1, //最少1个字符
            },
        },
        //错误信息提示
        messages: {
            driver: {
                required: "(必填)",
                minlength: "最少1位",
            },
            car: {
                required: "(必填)",
                minlength: "最少1位",
            },
            phone: {
                required: "(必填)",
                minlength: "最少1位",
            },
        },
    }).form();
}

/**
 * 图片上传
 */
function ImportImages() {
    if ($("#uploadImages").val() == "") {
        alert("请选择上传图片");
        return;
    }
    $.ajaxFileUpload({
        url: '/image/uploadImages',
        secureuri: false,
        fileElementId: "uploadImages",
        dataType: 'json',
        success: function (data, status) {
            img = data.url;
            //隐藏文件选择
            $("#uploadImages").css("display","none");
            //图片回显
            $("#showImage").css("display","");
            $("#showImage").attr("src",data.httpUrl);
        },
        error: function (data, status, e) {
            console.log("上传图片失败", e);
        }

    });
}